#include <pthread.h>
struct msg {
    struct msg *m_next;
    /* ... more stuff hre ... */
};

struct msg *workq;
pthread_mutex_t qlock= PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t qready= PTHREAD_COND_INITIALIZER;
 
void process_msg() {
    struct msg *mp;
    for (;;) {
        pthread_mutex_lock(&qlock);
        /* 就像书中所说，等待之前要进行条件判断，所以互斥量必须锁住*/
        while (workq == NULL)
            pthread_cond_wait(&qready, &qlock);
        mp= workq;
        workq= workq->m_next;
        pthread_mutex_unlock(&qlock);
        /* now process the message mp */
    }
}

void enqueue(struct msg *mp) {
    pthread_mutex_lock(&qlock);
    mp->m_next= workq;
    workq= mp;
    pthread_mutex_unlock(&qlock);
    /* 互斥量主要是锁住条件状态，至于发送条件信号，并不需要锁 */
    pthread_cond_signal(&qready);
}
